const jump = (
  to: null | number,
  current: number,
  total: number,
  toolbar: any
) => {
  return new Promise(resolve => {
    if (to === null && current !== total) {
      current = total;
      toolbar.previous.isDisable = false;
      toolbar.jumpFirst.isDisable = false;
      toolbar.next.isDisable = true;
      toolbar.jumpLast.isDisable = true;
    } else if (to === -1 && current !== 1) {
      current = 1;
      toolbar.previous.isDisable = true;
      toolbar.jumpFirst.isDisable = true;
      toolbar.next.isDisable = false;
      toolbar.jumpLast.isDisable = false;
    } else if (to > 1 && to < total) {
      current = to;
      toolbar.previous.isDisable = false;
      toolbar.jumpFirst.isDisable = false;
      toolbar.next.isDisable = false;
      toolbar.jumpLast.isDisable = false;
    }
    resolve(current);
  });
};

/**
 * 上一页
 */
const jumpPrev = (current: number, total: number, toolbar: any) => {
  const to = current - 1;
  if (to === 1) return jump(-1, current, total, toolbar);
  return jump(to, current, total, toolbar);
};

/**
 * 下一页
 */
const jumpNext = (current: number, total: number, toolbar: any) => {
  const to = current + 1;
  if (to === total) return jump(null, current, total, toolbar);
  return jump(to, current, total, toolbar);
};

/**
 * 跳页
 */
const jumpTo = (to: number, current: number, total: number, toolbar: any) => {
  if (to === -1) {
    return jumpPrev(current, total, toolbar);
  }
  if (to === null) {
    return jumpNext(current, total, toolbar);
  }
  if (to === 1) {
    return jump(-1, current, total, toolbar);
  }
  if (to === total) {
    return jump(null, current, total, toolbar);
  }
  return jump(to, current, total, toolbar);
};

export default {
  jumpTo
};
